package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.UserLocationApi;
import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void location(Double longitude, Double latitude, String addrStr, Long userId) {
        // 查看当前用户是否有地理位置
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if (userLocation == null) {
            // 没有 --保存
            userLocation = new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setAddress(addrStr);
            //创建一个经纬度对象(mongodb提供用来设置经纬度的对象)
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude);
            userLocation.setLocation(geoJsonPoint);
            userLocation.setCreated(System.currentTimeMillis());
            mongoTemplate.insert(userLocation);
        } else {
            // 有 修改
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude);
            Update update = new Update();
            update.set("address", addrStr);
            update.set("location", geoJsonPoint);
            update.set("updated", System.currentTimeMillis());
            mongoTemplate.upsert(query, update, "user_location");
        }

    }

    @Override //获取到自身距离半径的附近人
    public List<UserLocationVo> findNearUser(Integer distance, Long userId) {

        //1 查询出来自己的距离
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //2 按照指定的距离做半径搜索
        Distance dt = new Distance(distance / 1000, Metrics.KILOMETERS);

        Circle circle = new Circle(userLocation.getLocation(), dt);

        //3 查出来符合条件的人
        Query query2 = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> locationList = mongoTemplate.find(query2, UserLocation.class);

        //自身创建一个业务实现类  将附件人的实现类数据转换给业务实现类
        List<UserLocationVo> voList = UserLocationVo.formatToList(locationList);

        // 返回业务实现类
        return voList;
    }

    /**
     * 获取坐标
     *
     * @param userid
     * @return
     */
    @Override
    public Map<String, Double> getCoordinates(String userid) {
        Query query = new Query(Criteria.where("userId").is(Long.parseLong(userid)));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        GeoJsonPoint location = userLocation.getLocation();
        Map<String, Double> map = new HashMap<>();
        map.put("lng", location.getX());
        map.put("lat", location.getY());
        return map;
    }
}
